//
//  MSHomeTagsView.m
//  MS
//
//  Created by Yuan on 13-2-9.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSHomeTagsView.h"

@implementation TagsButton

-(id)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        
        titleLbl = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLbl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        titleLbl.font = FONT_SMALL;
        titleLbl.text = title;
        titleLbl.textColor = [UIColor darkGrayColor];
        titleLbl.numberOfLines = 1;
        titleLbl.textAlignment =UITextAlignmentCenter;
        titleLbl.backgroundColor = [UIColor clearColor];
        titleLbl.opaque = NO;
        [self addSubview:titleLbl];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    if ([titleLbl.text length]>0)
    {
        [titleLbl sizeToFit];
        [titleLbl setHidden:NO];
        CGRect titleFrame = titleLbl.frame;
        if (titleFrame.size.width > self.bounds.size.width - 4.0) {
            titleFrame.size.width = self.bounds.size.width - 4.0;
        }
        titleLbl.frame = titleFrame;
        titleLbl.center = CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds));
    }
    else
    {
        [titleLbl setHidden:YES];
    }
}


- (void)drawRect:(CGRect)rect
{    
    //top edge line
    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ref,1.0);
    CGContextSetRGBStrokeColor(ref, 0, 0, 0, 0.1);
    CGContextMoveToPoint(ref, self.frame.size.width, 0.0);
    CGContextAddLineToPoint(ref,self.frame.size.width,self.frame.size.height);
    CGContextStrokePath(ref);
}

@end

@implementation MSHomeTagsView

- (id)initWithFrame:(CGRect)frame tags:(NSArray *)tags
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"home_tagsbg"]];
        CGFloat btn_width = frame.size.width/[tags count];
        for (int i = 0 ; i < [tags count]; i++)
        {
            TagsButton *btn =[[TagsButton alloc]initWithFrame:CGRectMake(i*btn_width,0,btn_width,frame.size.height) title:[tags objectAtIndex:i]];
            [self addSubview:btn];
        }        
    }
    return self;
}

@end
